/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package ga.tsp;

import java.util.ArrayList;
import java.util.List;

import org.jheuristics.DefaultIndividual;
import org.jheuristics.Individual;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.IndividualsFactory;

public class TSPIndividualsFactory implements IndividualsFactory {
	private RouteEvaluator routeEvaluator;

	/**
	 * TODO
	 *
	 * @param evaluator
	 */
	public TSPIndividualsFactory(RouteEvaluator routeEvaluator) {
		setRouteEvaluator(routeEvaluator);
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.IndividualsFactory#createRandomIndividual(org.jheuristics.ga.GAConfig)
	 */
	public Individual createRandomIndividual(GAConfig config) {
		List avaliablePositions = new ArrayList(routeEvaluator.getGraph().getNodes());
		avaliablePositions.remove(routeEvaluator.getInitialPosition());

		List gens = new ArrayList(avaliablePositions.size());
		while (avaliablePositions.size() != 0) {
			gens.add(gens.size(), avaliablePositions.remove(config.getRandomGenerator().nextInt(avaliablePositions.size())));
		}
		return createSpecificIndividual(gens, config);
	}

	/* (non-Javadoc)
	 * @see org.jheuristics.ga.IndividualsFactory#createSpecificIndividual(java.lang.Object, org.jheuristics.ga.GAConfig)
	 */
	public Individual createSpecificIndividual(Object gens, GAConfig config) {
		return new DefaultIndividual(gens);
	}

	/**
	 * TODO
	 *
	 * @return
	 */
	public RouteEvaluator getRouteEvaluator() {
		return routeEvaluator;
	}

	/**
	 * TODO
	 *
	 * @param routeEvaluator
	 */
	public void setRouteEvaluator(RouteEvaluator routeEvaluator) {
		if (null == routeEvaluator) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		this.routeEvaluator = routeEvaluator;
	}

}
